﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IRCBot.Messages;

namespace IRCBot.Modules
{
    public class DrawingModule : IModule
    {
        public event SendDataEvent SendData;
        public event SayEvent Say;
        public event DelayedSayEvent DelayedSay;
        public event ModuleEvent ClearDelayedMessages;

        private string _theme = "";
        private string _participants = "";
        private double _timer = 5;

        public void HandleIncomingMessage(IncomingMessage message)
        {
            switch (message.Command.ToLower())
            {
                case ":!theme":
                    HandleTheme(message);
                    break;
                case ":!timer":
                    HandleTimer(message);
                    break;
                case ":!participants":
                    HandleParticipants(message);
                    break;
                case ":!start":
                    HandleStart(message);
                    break;
                case ":!stop":
                    HandleStop(message);
                    break;
            }
        }

        private void HandleStart(IncomingMessage message)
        {
            if (string.IsNullOrEmpty(_theme))
                Say("You didn't set a theme yet.", message.Channel);
            else if (string.IsNullOrEmpty(_participants))
                Say("You didn't set any participants, jerk.", message.Channel);
            else
            {
                Say("▄▀▄▀▄▀▄▀▄ NEW ROUND IS ABOUT TO START! ▄▀▄▀▄▀▄▀▄", message.Channel);
                Say("You have " + _timer + " minutes to draw ' " + _theme + " '", message.Channel);

                DateTime timeUp = DateTime.Now.AddMinutes(_timer).AddSeconds(8);
                DelayedSay("3", message.Channel, DateTime.Now.AddSeconds(2));
                DelayedSay("2", message.Channel, DateTime.Now.AddSeconds(4));
                DelayedSay("1", message.Channel, DateTime.Now.AddSeconds(6));
                DelayedSay("GO!", message.Channel, DateTime.Now.AddSeconds(8));
                DelayedSay("PENCILS DOWN! " + _participants, message.Channel, timeUp);
            }
        }

        private void HandleStop(IncomingMessage message)
        {
            ClearDelayedMessages();
            Say("The party has been pooped on. " + _participants, message.Channel);
        }

        private void HandleTheme(IncomingMessage message)
        {
            string newTheme = message.Parameters;

            if (string.IsNullOrEmpty(newTheme) == false)
                _theme = newTheme;
            Say("Theme is set to: " + _theme, message.Channel);
        }

        private void HandleTimer(IncomingMessage message)
        {
            string newTimer = message.GetParameterNumber(0);
            double oldTimer = _timer;

            if (string.IsNullOrEmpty(newTimer) == false)
                double.TryParse(newTimer, out _timer);

            if (_timer < 0)
            {
                Say("WE CAN'T GO BACK IN TIME!", message.Channel);
                _timer = oldTimer;
            }
            else if (_timer > 2000)
            {
                Say("IT'S TOO BIG", message.Channel);
                _timer = oldTimer;
            }

            Say("Timer is set to: " + _timer + " minutes.", message.Channel);
        }

        private void HandleParticipants(IncomingMessage message)
        {
            string newParticipants = message.Parameters;

            if (string.IsNullOrEmpty(newParticipants) == false)
                _participants = newParticipants;
            Say("Participants are: " + _participants, message.Channel);
        }

    }
}
